﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Interface;
using Model;

namespace DAL.Dao
{
    public class ComplaintBagDaoimpl : Base, IComplaintBagDao
    {
        public int ExistNum { get { return DbEntity.expressDBEntities.Complaint.Count(); } private set { } }

        public ComplaintBagDaoimpl()
        {
            DbEntity.CheckConn();
        }
       

        public Complaint Find(int id)
        {
            DbEntity.CheckConn();
            return DbEntity.expressDBEntities.Complaint.Find(id);
        }

        public int GetSenderUserIDByBagID(string bagID)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.UserInfo
                    from j in DbEntity.expressDBEntities.Complaint
                    where (bagID.Equals(j.BagID)) && i.TrueName.Equals(j.TrueName)
                    select i
                     ).Select(o => o.ID).ToList().FirstOrDefault();

        }

        public int GetSenderUserIDByComplaintID(int complaintID)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.UserInfo
                    from j in DbEntity.expressDBEntities.Complaint
                    from k in DbEntity.expressDBEntities.Bag
                    where j.ID==complaintID && k.BagID.Equals(j.BagID) && k.SenderName.Equals(i.UserName)
                    select i
                     ).Select(o => o.ID).ToList().FirstOrDefault();
        }


        public int GetGetterUserIDByComplaintID(int complaintID)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.UserInfo
                    from k in DbEntity.expressDBEntities.Bag
                    from j in DbEntity.expressDBEntities.Complaint
                    where j.ID == complaintID  && k.BagID.Equals(j.BagID)&& k.GetterName.Equals(i.UserName)
                    select  i
                     ).Select(o => o.ID).ToList().FirstOrDefault();
        }

        public bool Update(int id,Complaint newcomplaint)
        {
            try
            {
                DbEntity.CheckConn();
                Complaint bag = (from i in DbEntity.expressDBEntities.Complaint
                           where i.ID.Equals(id)
                           select i).ToList().FirstOrDefault();
                if (bag == null)
                {
                    return false;
                }



                //DbEntityEntry<Bag> entry = DbEntity.expressDBEntities.Entry<Bag>(newbag);
                //3.设置该对象为被修改过
                //entry.State = EntityState.Unchanged;
                //4.设置该对象的 uName属性为修改状态，同时 entry.State 被修改为 Modified 状态
                //bag.Title = newcomplaint.Title;
                bag.Info = newcomplaint.Info;
                //bag.IDCard = newcomplaint.IDCard;
                //bag.Phone = newcomplaint.Phone;
                //bag.TrueName = newcomplaint.TrueName;
                bag.ReplyInfo = newcomplaint.ReplyInfo;
                bag.ReplyTime = newcomplaint.ReplyTime;
                //bag.UpdateTime = newcomplaint.UpdateTime;
                
                //entry.State = EntityState.Modified;
                //5.关闭EF实体合法性检查（如果创建出来的要修改的数据有的字段没有赋值则关闭实体合法性检查，如果所有字段都赋值了则不用关闭EF实体合法性检查）

                //DbEntity.expressDBEntities.Configuration.ValidateOnSaveEnabled = false;

                //6.重新保存到数据库 -- ef 上下文会根据实体对象的状态，根据 entry.State =Modified 的值生成对应的 update sql 语句.

                DbEntity.expressDBEntities.SaveChanges();

            }
            catch (Exception)
            {

                return false;
            }
           
            return true;
        }
        public bool Delete(int id)
        {
            DbEntity.CheckConn();
            Complaint complaint = DbEntity.expressDBEntities.Complaint.Find(id);
            if (complaint == null) return false;
            DbEntity.expressDBEntities.Complaint.Remove(complaint);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }

        public bool Delete(ComplaintBag complaint)
        {
            DbEntity.CheckConn();
            DbEntity.expressDBEntities.Complaint.Remove(complaint);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }

        public (int,IList<ComplaintBag>)GetList(int rows, int page)
        {
            DbEntity.CheckConn();
            var rs = (from i in DbEntity.expressDBEntities.Complaint 
                      from j in DbEntity.expressDBEntities.Bag
                      where i.BagID.Equals(j.BagID)
                      orderby i.ID
                      select new ComplaintBag()
                      {
                          ID = i.ID,
                          Title = i.Title,
                          Info = i.Info,
                          IDCard = i.IDCard,
                          Phone = i.Phone,
                          TrueName = i.TrueName,
                          SubmitTime = i.SubmitTime,
                          ReplyInfo = i.ReplyInfo,
                          ReplyTime = i.ReplyTime,
                          BagID = i.BagID,
                          AddTime = j.AddTime,
                          UpdateTime = j.UpdateTime
                      }
                      ).ToList();

            return (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());
        }

        public  IList<ComplaintBag> GetList(string username, string phone)
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.Complaint
                    from j in DbEntity.expressDBEntities.Bag
                    where i.Phone.Equals(phone) &&i.BagID.Equals(j.BagID)&& (j.GetterName.Equals(username) || j.SenderName.Equals(username))
                    select new ComplaintBag()
                    {
                        ID = i.ID,
                        Title = i.Title,    
                        Info = i.Info,
                        IDCard = i.IDCard,
                        Phone = i.Phone,
                        TrueName=i.TrueName,
                        SubmitTime= i.SubmitTime,
                        ReplyInfo= i.ReplyInfo,
                        ReplyTime=i.ReplyTime,
                        BagID=i.BagID,
                        AddTime = j.AddTime,
                        UpdateTime= j.UpdateTime
                    }
                     ).ToList();
        }


        public (int,IList<ComplaintBag>) GetList(string username,string phone,int rows,int page)
        {
            DbEntity.CheckConn();
            var rs = (from i in DbEntity.expressDBEntities.Complaint
                                      from j in DbEntity.expressDBEntities.Bag
                      where i.BagID.Equals(j.BagID) && i.Phone.Equals(phone) && (j.GetterName.Equals(username) || j.SenderName.Equals(username))
                      select new ComplaintBag()
                      {
                          ID = i.ID,
                          Title = i.Title,
                          Info = i.Info,
                          IDCard = i.IDCard,
                          Phone = i.Phone,
                          TrueName = i.TrueName,
                          SubmitTime = i.SubmitTime,
                          ReplyInfo = i.ReplyInfo,
                          ReplyTime = i.ReplyTime,
                          BagID = i.BagID,
                          AddTime = j.AddTime,
                          UpdateTime = j.UpdateTime
                      }
                     ).ToList();



            return  (GetTotalPages(rs.Count, rows), rs.Skip((page - 1) * rows).Take(rows).ToList());

        }





        public IList<ComplaintBag> GetList()
        {
            DbEntity.CheckConn();
            return (from i in DbEntity.expressDBEntities.Complaint
                    from j in DbEntity.expressDBEntities.Bag
                    where i .BagID.Equals(j.BagID)
                    orderby i.ID
                    select new ComplaintBag()
                    {
                        ID = i.ID,
                        Title = i.Title,
                        Info = i.Info,
                        IDCard = i.IDCard,
                        Phone = i.Phone,
                        TrueName = i.TrueName,
                        SubmitTime = i.SubmitTime,
                        ReplyInfo = i.ReplyInfo,
                        ReplyTime = i.ReplyTime,
                        BagID = i.BagID,
                        AddTime = j.AddTime,
                        UpdateTime = j.UpdateTime
                    }).ToList();
        }
     

        public bool Add(Complaint complaint)
        {
            DbEntity.CheckConn();
            DbEntity.expressDBEntities.Complaint.Add(complaint);
            DbEntity.expressDBEntities.SaveChanges();
            return true;
        }

        public void Dispose()
        {
            if (DbEntity.expressDBEntities != null) DbEntity.expressDBEntities.Database.Connection.Close();
        }


    }
}
